package org.stvd.repository.admin.impl;

import java.util.List;

import org.springframework.stereotype.Repository;
import org.stvd.core.util.StringUtil;
import org.stvd.entities.admin.RoleMenu;
import org.stvd.repository.admin.RoleMenuDao;
import org.stvd.repository.base.impl.BaseDaoImpl;

@Repository("RoleMenuDao")
public class RoleMenuDaoImpl extends BaseDaoImpl<RoleMenu> implements RoleMenuDao {

    @Override
    public List<RoleMenu> findRoleMenuByRid(String roleId, String guid) {
        return findByHQL("from RoleMenu T where T.roleId= ?0 and T.guid = ?1", roleId, guid);
    }

    @Override
    public void insertRoleMenu(String roleId, String[] menuids, String guid) {
        em.createQuery("delete from RoleMenu T where T.roleId = :roleId and T.guid = :guid")
        .setParameter("roleId", roleId)
        .setParameter("guid", guid)
        .executeUpdate();
        em.flush();
        if (menuids != null) {
            for (String menuId : menuids) {
                RoleMenu menuroleTmp = new RoleMenu();
                menuroleTmp.setId(StringUtil.getRandomUUID());
                menuroleTmp.setRoleId(roleId);
                menuroleTmp.setMenuId(menuId);
                menuroleTmp.setGuid(guid);
                insert(menuroleTmp);
            }
        }
    }
}
